import * as types from '../../mutation-types'

//专栏列表
export const initForumList = ({ commit, state, rootState }, callback) => {

    f7.httpGet(
        rootState.cache.api + rootState.request.get_forum_list, {},
        function(dataset) {
            //数据准备
            const data = dataset.data
            data.serverTime = dataset.serverTime

            //专栏数据状态入库
            commit(types.UPDATE_FORUM, data)

            if (callback) callback();
        }
    )


}

//专栏详情
export const initForumPage = ({ commit, state, rootState }, callback) => {

    f7.httpGet(
        rootState.cache.api + rootState.request.get_forum_page, { forum_id: rootState.current.forumId, uid: rootState.userProfile.uid },
        function(dataset) {
            //数据准备
            const data = dataset.data
            data.serverTime = dataset.serverTime

            //文章列表状态入库
            if (_.has(data, 'topic') && !_.isNull(data.topic)) {
                commit(types.UPDATE_TOPIC, data.topic)
            }

            //专栏数据状态入库
            commit(types.UPDATE_FORUM, data)

            if (callback) callback();
        }
    )

}


//文章详情
export const initArticlePage = ({ commit, state, rootState }, callback) => {

    f7.httpGet(
        rootState.cache.api + rootState.request.get_topic_page, { topic_id: rootState.current.topicId, uid: rootState.userProfile.uid },
        function(dataset) {
            const data = dataset.data
            data.serverTime = dataset.serverTime

            //文章状态入库
            commit(types.UPDATE_TOPIC, data)

            if (callback) callback();
        }
    )


}


//赞了文章
//export const updateTopicAsLiked = ({ commit, state, rootState }, callback) => {
//
//    var liked = true;
//
//    commit(types.UPDATE_TOPIC_AS_LIKED);
//
//    const recordIndex = _.findLastIndex(rootState.topic.list, function(obj){
//        return rootState.current.topicId == obj.id
//    })
//
//    if(recordIndex != -1){
//        liked = rootState.topic.list[recordIndex].visitorStatus.isLike;
//    }
//
//    f7.httpGet(
//        rootState.cache.api + rootState.request.set_topic_as_liked,
//        {topicId: rootState.current.topicId, liked: liked, uid: rootState.userProfile.uid}
//    )
//}


//搜索结果
export const initSearchList = ({ commit, state, rootState }, callback) => {


}